package org.example.struct.array;

import java.util.HashSet;
import java.util.Set;

/**
 * @author liyishan
 * @date 2024/7/1 10:28
 * @apiNote
 */

public class Test00874RobotSim {
    public int robotSim(int[] commands, int[][] obstacles) {
        int[][] dirs = {{-1,0},{0,1},{1,0},{0,-1}};
        int px = 0, py = 0,d=1;
        Set<Integer> set = new HashSet<>();
        for(int[] obstacle:obstacles){
            set.add(obstacle[0] * 60001 + obstacle[1]);
        }
        int res = 0;
        for(int c :commands){
            if(c < 0){
                d += c == -1 ? 1 : -1;
                d %= 4;
                if(d < 0){
                    d += 4;
                }
            }else{
                for (int i = 0; i < c; i++) {
                    if (set.contains((px + dirs[d][0]) * 60001 + py + dirs[d][1])) {
                        break;
                    }
                    px += dirs[d][0];
                    py += dirs[d][1];
                    res = Math.max(res, px * px + py * py);
                }
            }
        }
        return res;
    }
}
